***************************************
*******START HERE***********************
*****************************************

*** DESCRIPTIVE STATS DATASET
*Eliminate countries with just one period merged
duplicates tag country_name new_party_name new_party_code, gen(drop)
drop if drop==0
drop drop

*Generate number id for countries and parties
sort country_name new_party_code year mn
egen ctr_id = group( country_name)
egen new_pid = group( country_name new_party_code new_party_name)

*Eliminate parties with just one run election
duplicates tag country_name new_party_code new_party_name, g(dupli)
drop if dupli==0

tab ctr_id
tab new_pid

*Parties by countries and years
bysort country:tab new_party_name
bysort year: tab country_name

*Times per political party
sort new_pid year
bysort new_pid: gen times = _n

*NEW VARIABLES AND CODIFICATIONS

*Region
label define reg1 1"Europa" 2"Africa" 3"Asia" 4"Oceania" 5"America"
label values region reg1

*Candidate nomination
label define nom0 0"Party leader unilaterally" 1"National party leadership" 2"Delegates of local/regional" 3"All party members" 4"All registered voters"
label values v2panom_ord nom0

*Centralization
recode v2panom_ord (0 1=1) (2 3 4=0), gen(nomin1)
label variable nomin1 "Candidate Selection Centralization (Binary)"
label define nom1 0"Decentralized" 1"Centralized" 
label values nomin1 nom1

*Nomination 3 categories
recode v2panom_ord (0 1=1) (3 4=3), gen(nomin2)
label variable nomin2 "Candidate Selection (3 Categories)"
label define nom2 1"National Leaders" 2"Subnational Leaders" 3"Primaries" 
label values nomin2 nom2

*Electoral System
gen elecsys=.
replace elecsys=1 if pluralty==1
replace elecsys=2 if (pr==1 & cl==1 & elecsys==.)
replace elecsys=3 if (pr==1 & cl==0 & elecsys==.)
replace elecsys=4 if (pr==1 & pluralty==1)
label variable elecsys "Electoral System (4 categories - DPI)"

label define elsys 1"Plurality" 2"PR Closed List" 3"PR Open List" 4"Mixed"
label values elecsys elsys

recode v2elparlel (0 = 1) (1 = 2) (2 = 4) (3 = 5), g(elecsys2)
replace elecsys2= 3 if (v2elparlel==1 & cl==1)

label define elsys2 1"Majoritarian" 2"OLPR" 3"CLPR" 4"Mixed" 5"Other"
label values elecsys2 elsys2

*****Set dataset as panel data to run models

sort new_pid times
by new_pid: gen pns1 = PNS_sw[_n-1]
by new_pid: gen v2pavote2 = v2pavote[_n-1]
by new_pid: gen v2padisa_ord2 = v2padisa_ord[_n-1]
by new_pid: gen v2pariglef_osp2 = v2pariglef_osp[_n-1]


*Final Models
cd "C:\Users\L03541284\OneDrive - Instituto Tecnologico y de Estudios Superiores de Monterrey\Documents\Research_Projects\Candidate_Selection\Results_v2"
eststo clear
logit nomin1 pns1 i.region i.year, or vce(cluster new_pid)
eststo
logit nomin1 pns1 i.elecsys2 v2pavote2 v2pariglef_osp2 partyage mdmh i.region i.year, or vce(cluster new_pid)
eststo
logit nomin1 c.pns1##i.elecsys2 v2pavote2 v2pariglef_osp2 partyage mdmh i.region i.year, or vce(cluster new_pid)
eststo
logit nomin1 c.pns1##i.elecsys2 v2pavote2 v2pariglef_osp2 partyage mdmh v2x_polyarchy i.region i.year, or vce(cluster new_pid)
eststo

esttab using "Table1_elecsys.csv", nogaps se star(* 0.10 ** 0.05 *** 0.01) drop(*.year *.region)mtitles("M1" "M2" "M3" "M4" "M5" "M6" "M7") scalars(N chi2) b(%7.3f) se(%7.2f) sfmt(%7.2f) noomit nobase eform compress replace

***###Graph by electoraly systems
qui logit nomin1 c.pns1##i.elecsys2 v2pavote2 v2pariglef_osp2 partyage mdmh v2x_polyarchy i.region i.year, or vce(cluster new_pid)


**Discrete change
mchange pns1, atmeans stats(ci) centered
mchange elecsys2, atmeans stats(ci) centered
margins, dydx(elecsys2) at(pns1=(0(.05)1)) vsquish


**Margins
mgen, at(pns1=(0(.05)1)) atmeans stub(S0) level(95)
label var S0pr1 "Total"
mgen, at(elecsys2=1 pns1=(0(.05)1)) atmeans stub(S1) level(90)
label var S1pr1 "Majoritarian"
mgen, at(elecsys2=2 pns1=(0(.05)1)) atmeans stub(S2) level(90)
label var S2pr1 "OLPR"
mgen, at(elecsys2=3 pns1=(0(.05)1)) atmeans stub(S3) level(90)
label var S3pr1 "CLPR"
mgen, at(elecsys2=4 pns1=(0(.05)1)) atmeans stub(S4) level(90)
label var S4pr1 "Mixed" 
mgen, at(elecsys2=5 pns1=(0(.05)1)) atmeans stub(S5) level(90)
label var S5pr1 "Other"

*Graph Predicted Probabilities
graph twoway ///
      (rarea S0ul1 S0ll1 S0pns1, color(black%40)) ///
      (connected S0pr1 S0pns1, lpattern(solid) lcolor(black) msymbol(none)), ///
      legend(off) ///
      ylabel(0(.2)1, labsize(vsmall)) ytitle("Pr(Centralization = 1)", size(small)) ///
      xlabel(0(.1)1, labsize(vsmall)) xtitle("Party Nationalization Score", size(small)) ///
	  title("") name(A0)
graph export predicted_prob1.png, replace

**Graph Interaction Effect
margins elecsys2, dydx( pns1 ) /*Slopes for each electoral system */
qui margins elecsys2, at(pns1=(0(.05)1))
marginsplot, recast(line) noci ///
             plot1opts(lpattern(solid)lc(black)) ///
			 plot2opts(lpattern(dash)lc(black)) ///
			 plot3opts(lpattern(shortdash)lc(black)) ///
			 plot4opts(lpattern(shortdash_dot)lc(black)) ///
			 plot5opts(lpattern(dot) lc(black))   ///
			 xlabel(0(.1)1,labsize(vsmall)) ylabel(0(.2)1,labsize(vsmall))  ///
			 xtitle("Party Nationalization Score",size(small)) ytitle("Pr(Centralization = 1)",size(small)) title("") ///
			 legend(on size(vsmall) c(3))
graph export predicted_prob2.png, replace


***Graph predicted probabilities by electoral system
graph twoway ///
      (rarea S1ul1 S1ll1 S1pns1, color(black%30)) ///
      (connected S1pr1 S1pns1, lpattern(solid) lcolor(black) msymbol(none)), ///
      legend(off) ///
      ylabel(-.4(.2)1, labsize(vsmall)) ytitle("Pr(Centralization = 1)", size(small)) ///
      xlabel(0(.1)1, labsize(vsmall)) xtitle("Party Nationalization Score", size(small)) ///
	  title("Majoritarian", size(small)) name(A1)
	  
graph twoway ///
      (rarea S2ul1 S2ll1 S2pns1, color(black%30)) ///
      (connected S2pr1 S2pns1, lpattern(dash) lcolor(black) msymbol(none)), ///
      legend(off) ///
      ylabel(-.4(.2)1, labsize(vsmall)) ytitle("Pr(Centralization = 1)", size(small)) ///
      xlabel(0(.1)1, labsize(vsmall)) xtitle("Party Nationalization Score", size(small)) ///
	  title("OLPR", size(small)) name(A2)
	  
graph twoway ///
      (rarea S3ul1 S3ll1 S3pns1, color(black%30)) ///
      (connected S3pr1 S3pns1, lpattern(shortdash) lcolor(black) msymbol(none)), ///
      legend(off) ///
      ylabel(-.4(.2)1, labsize(vsmall))  ytitle("Pr(Centralization = 1)", size(small)) ///
      xlabel(0(.1)1, labsize(vsmall)) xtitle("Party Nationalization Score", size(small)) ///
	  title("CLPR", size(small)) name(A3)
	  
graph twoway ///
      (rarea S4ul1 S4ll1 S4pns1, color(black%30)) ///
      (connected S4pr1 S4pns1, lpattern(shortdash_dot) lcolor(black) msymbol(none)), ///
      legend(off) ///
      ylabel(-.4(.2)1, labsize(vsmall)) ytitle("Pr(Centralization = 1)", size(small)) ///
      xlabel(0(.1)1, labsize(vsmall)) xtitle("Party Nationalization Score", size(small)) ///
	  title("Mixed", size(small)) name(A4)
	  
graph twoway ///
      (rarea S5ul1 S5ll1 S5pns1, color(black%30)) ///
      (connected S5pr1 S5pns1, lpattern(dot) lcolor(black) msymbol(none)), ///
      legend(off) ///
      ylabel(-.4(.2)1, labsize(vsmall)) ytitle("Pr(Centralization = 1)", size(small)) ///
      xlabel(0(.1)1, labsize(vsmall)) xtitle("Party Nationalization Score", size(small)) ///
	  title("Other", size(small)) name(A5)
	  
graph combine A1 A2 A3 A4 A5, iscale(.5) name(comb2) ycommon
graph close A1 A2 A3 A4 A5
graph export predicted_prob3.png, replace width(6000)


*** Graph comparisons among slopes
graph twoway ///
      (rarea S1ul1 S1ll1 S1pns1, color(black%30)) ///
      (rarea S2ul1 S2ll1 S2pns1, color(black%30)) ///
      (connected S1pr1 S1pns1, lpattern(solid) lcolor(black) msymbol(none)) ///
      (connected S2pr1 S2pns1, lpattern(dash) lcolor(black) msymbol(none)), ///
      legend(on order(3 4) size(small)) ///
      ylabel(-.4(.2)1, labsize(vsmall)) ytitle("Pr(Centralization = 1)", size(small)) ///
      xlabel(0(.1)1, labsize(vsmall)) xtitle("Party Nationalization Score", size(small)) ///
	  title("Majoritarian Vs. OLPR", size(small)) name(E1)
	  
graph twoway ///
      (rarea S1ul1 S1ll1 S1pns1, color(black%30)) ///
      (rarea S3ul1 S3ll1 S3pns1, color(black%30)) ///
      (connected S1pr1 S1pns1, lpattern(solid) lcolor(black) msymbol(none)) ///
      (connected S3pr1 S3pns1, lpattern(shortdash) lcolor(black) msymbol(none)), ///
      legend(on order(3 4) size(small)) ///
      ylabel(-.4(.2)1, labsize(vsmall))  ytitle("Pr(Centralization = 1)", size(small)) ///
      xlabel(0(.1)1, labsize(vsmall)) xtitle("Party Nationalization Score", size(small)) ///
	  title("Majoritarian Vs. CLPR", size(small)) name(E2)
	  
graph twoway ///
      (rarea S1ul1 S1ll1 S1pns1, color(black%30)) ///
      (rarea S4ul1 S4ll1 S4pns1, color(black%30)) ///
      (connected S1pr1 S1pns1, lpattern(solid) lcolor(black) msymbol(none)) ///
      (connected S4pr1 S4pns1, lpattern(shortdash_dot) lcolor(black) msymbol(none)), ///
      legend(on order(3 4) size(small)) ///
      ylabel(-.4(.2)1, labsize(vsmall)) ytitle("Pr(Centralization = 1)", size(small)) ///
      xlabel(0(.1)1, labsize(vsmall)) xtitle("Party Nationalization Score", size(small)) ///
	  title("Majoritarian Vs. Mixed", size(small)) name(E3)
	  
graph twoway ///
      (rarea S1ul1 S1ll1 S1pns1, color(black%30)) ///
      (rarea S5ul1 S5ll1 S5pns1, color(black%30)) ///
      (connected S1pr1 S1pns1, lpattern(solid) lcolor(black) msymbol(none)) ///
      (connected S5pr1 S5pns1, lpattern(dot) lcolor(black) msymbol(none)), ///
      legend(on order(3 4) size(small)) ///
      ylabel(-.4(.2)1, labsize(vsmall)) ytitle("Pr(Centralization = 1)", size(small)) ///
      xlabel(0(.1)1, labsize(vsmall)) xtitle("Party Nationalization Score", size(small)) ///
	  title("Majoritarian Vs. Other", size(small)) name(E4)
	  

*Now combining the plots together
graph combine E1 E2 E3 E4, iscale(.5) name(comb1) ycommon
graph close E1 E2 E3 E4
graph export predicted_prob4.png, replace width(6000) 
